import baseApi from './baseApi';

// 图表列表接口
export interface ChartListItem {
  id: string;
  title: string;
}

// 图表数据请求参数
export interface ChartDataParams {
  store: number;
  chartId: string;
  timeRangeStart?: string;
  timeRangeEnd?: string;
  timeGrain?: string;
  filters?: any[];
  sortByField?: string;
  sortByOrder?: string;
  groupBy?: string;
  limit?: number;
}

// 图表数据响应
export interface ChartDataResponse {
  id: string;
  chartType: string;
  title: string;
  subtitle: string;
  tags: string[];
  defaultTimeRange: string[];
  meta: Record<string, {
    alias: string;
    unit?: string;
  }>;
  data: Record<string, any>[];
  xField: string;
  yField: string;
}

// 汇总数据响应
export interface SummaryDataResponse {
  chart: ChartDataResponse;
  summary: {
    gmv: number | null;
    rate: number;
    same_rate: number;
    unit: string;
    meta: Record<string, {
      alias: string;
      unit: string;
    }>;
  };
}

// API 响应接口
export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T | null;
}

export const dataService = {
  // 获取图表列表
  async getChartList(): Promise<ApiResponse<ChartListItem[]>> {
    try {
      const response = await baseApi.get('/admin/data/charts');
      return response.data;
    } catch (error) {
      return {
        code: -1,
        message: '获取图表列表失败',
        data: null
      };
    }
  },

  // 获取图表数据
  async getChartData(params: ChartDataParams): Promise<ApiResponse<ChartDataResponse>> {
    try {
      const response = await baseApi.post('/admin/data/chart', params);
      return response.data;
    } catch (error) {
      return {
        code: -1,
        message: '获取图表数据失败',
        data: null
      };
    }
  },

  // 获取汇总数据
  async getSummaryData(params: ChartDataParams): Promise<ApiResponse<SummaryDataResponse>> {
    try {
      const response = await baseApi.post('/admin/data/summary', params);
      return response.data;
    } catch (error) {
      return {
        code: -1,
        message: '获取汇总数据失败',
        data: null
      };
    }
  }
}; 